package com.eqxiu.ieditor.api.controller.mall;

import com.eqxiu.ieditor.api.controller.BaseApiController;
import com.eqxiu.ieditor.api.controller.BaseController;
import com.eqxiu.ieditor.commons.web.ResultData;
import com.eqxiu.ieditor.dto.MallUserDTO;
import com.eqxiu.mall.search.api.IMallSearchServiceApi;
import com.eqxiu.mall.search.dto.RecommendDTO;
import com.eqxiu.tiny.pagination.PageDTO;
import com.google.gson.Gson;
import eqxiu.mall.product.dto.ProductDTO;
import eqxiu.mall.product.dto.UserDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/***
 * @description 猜你喜欢
 * @author tanghao
 * @date 2020年1月6日
 */
@RestController
@RequestMapping("/api/wap")
public class RecommendSearchController extends BaseController implements BaseApiController {

    Logger logger = LoggerFactory.getLogger(RecommendSearchController.class);

    @Autowired
    private IMallSearchServiceApi mallSearchServiceApi;


    /**
     * @throws
     * @description wap猜你喜欢(调用大数据接口)
     * @author tanghao
     * @date 2020/1/6
     */
    @PostMapping("/findrecommend")
    @ResponseBody
    public ResultData findDetailFavorite(HttpServletRequest request, String keywords, String positionId, String excludeIds, Integer pageNo, Integer pageSize) {
        logger.info("this is keywords ={},positionId ={},excludeIds={}", keywords, positionId,excludeIds);
        List result = new ArrayList();
        Map<String, Object> extra = new HashMap<>(16);
        RecommendDTO recommendDTO = new RecommendDTO();
        recommendDTO.setUserId(geUserId(request));
        recommendDTO.setIp(this.getIpAddr(request));
        recommendDTO.setPositionId(positionId);
        //关键词
        if (StringUtils.isNotBlank(keywords)) {
            extra.put("searchStr", keywords);
        }
        //商品去重
        if (StringUtils.isNotBlank(excludeIds)) {
            String[] ids = excludeIds.split(",");
            extra.put("excludeIds", Arrays.asList(ids));
        }
        extra.put("size", pageSize);
        extra.put("page", pageNo);
        recommendDTO.setExtra(extra);
        logger.info("this is recommend dto={}", new Gson().toJson(recommendDTO));
        try {
            PageDTO<ProductDTO> recommendDTOs = mallSearchServiceApi.searchTemplateRecAndMaterialRec(recommendDTO, true);
            result = recommendDTOs.getDataList();
            logger.info("this is recommend result={}", new Gson().toJson(result));
        } catch (Exception e) {
            logger.info("this is info={}", e.getMessage());
        }
        return success().setList(result);
    }

    /**
     * @Description 封装用户信息
     * @Author SuiXiangyu
     * @Date 2019/8/5
     **/
    private UserDTO getUserDto(MallUserDTO user) {
        UserDTO userDTO = new UserDTO();
        userDTO.setLoginName(user.getLoginName());
        userDTO.setId(user.getId());
        return userDTO;
    }
}
